package com.mine.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.dao.AddressMapper;
import com.mine.dao.MineAddressMapper;
import com.mine.pojo.Address;
import com.mine.pojo.MineAddress;
import com.mine.pojo.MineAddressExample;
import com.mine.pojo.MineAddressExample.Criteria;
import com.mine.pojo.MineUser;
import com.mine.result.CodeMsg;
import com.mine.result.Result;

@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	AddressMapper addressMapper;
	
	@Autowired
	UserService userService;
	
	@Autowired
	MineAddressMapper mineAddressMapper;

	@Override
	public Result selectAddressList(String token) {
		
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//List<Address> list = addressMapper.selectAddressListByUserId(user.getId());
		MineAddressExample example = new MineAddressExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		List<MineAddress> selectByExample = mineAddressMapper.selectByExample(example);
		return Result.success(selectByExample);
	}

	@Override
	public Result deleteAddressById(String token, Address address) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		addressMapper.deleteAddressById(address);
		return Result.ok();
	}

	@Override
	public Result updateAddress(String token, Address address) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		address.setUserId(user.getId());
		address.setUpdateTime(new Date());
		addressMapper.updateAddress(address);
		return Result.ok();
	}

	@Override
	public Result addAddress(String token, Address address) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		address.setUserId(user.getId());
		address.setCreateTime(new Date());
		address.setUpdateTime(new Date());
		address.setPriority((byte) 0);
		addressMapper.addAddress(address);
		return Result.success(address);
	}

	@Override
	public Result updatePriorityAddress(String token, Address address) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		address.setPriority((byte) 1);
		List<Address> list = addressMapper.selectAddressListByUserId(user.getId());
		for (int i = 0; i < list.size(); i++) {
			Address addTmp = list.get(i);
			if (addTmp.getId().equals(address.getId())) {
				addTmp.setPriority((byte)1);
			}else {
				addTmp.setPriority((byte)0);
			}
		}
		System.out.println(list);
		addressMapper.updatePriorityAddressList(list);
		return Result.ok();
	}

	@Override
	public Result getPriorityAddress(String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//List<Address> list = addressMapper.selectAddressListByUserId(user.getId());
		MineAddressExample example = new MineAddressExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		criteria.andPriorityEqualTo((byte)1);
		List<MineAddress> selectByExample = mineAddressMapper.selectByExample(example);

		return Result.success(selectByExample);
	}
}
